package com.rybbaby.tss.front.tssMemberAddSubtractRecord.service.imp;

import com.google.common.collect.ImmutableMap;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssClassesLessons.service.ITssClassesLessonsService;
import com.rybbaby.tss.front.tssMemberAddSubtractRecord.dao.ITssMemberAddSubtractRecordDao;
import com.rybbaby.tss.front.tssMemberAddSubtractRecord.entity.TssMemberAddSubtractRecord;
import com.rybbaby.tss.front.tssMemberAddSubtractRecord.service.ITssMemberAddSubtractRecordService;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.entity.TssMemberAttendClassRecord;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.service.ITssMemberAttendClassRecordService;
import com.rybbaby.tss.front.tssMemberCheckAttendance.entity.TssMemberCheckAttendance;
import com.rybbaby.tss.front.tssMemberCheckAttendance.service.ITssMemberCheckAttendanceService;
import com.rybbaby.tss.front.tssMemberPackage.entity.TssMemberPackage;
import com.rybbaby.tss.front.tssMemberPackage.service.ITssMemberPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 雷春琼
 * @description 会员加减课时记录表数据库操作服务实现
 * @createDate 2017-08-09 下午11:59:34
 */
@Service
@Transactional
public class TssMemberAddSubtractRecordServiceImp extends BaseServiceImp<TssMemberAddSubtractRecord> implements ITssMemberAddSubtractRecordService {

    @Autowired
    private ITssMemberPackageService packageService;
    @Autowired
    private ITssMemberAddSubtractRecordDao dao;
    @Autowired
    private ITssMemberPackageService tssMemberPackageService;
    @Autowired
    private ITssMemberCheckAttendanceService tssMemberCheckAttendanceService;
    @Autowired
    private ITssClassesLessonsService tssClassesLessonsService;
    @Autowired
    private ITssMemberAttendClassRecordService tssMemberAttendClassRecordService;


    @Override
    public Boolean save(TssMemberAddSubtractRecord entity,String adminUserId) throws Exception {
        boolean bool = true;
        TssMemberPackage memberPackage = packageService.get(entity.getMemberPackageId());
        Map<String, Object> map = new HashMap<>();
        Integer classHour = entity.getClassHour();
        Integer row = 0;
        //获取剩余课时数
        Integer surplusClassHour = memberPackage.getSurplusClassHour();

        Integer addSubtractType=entity.getAddSubtractType();
        Integer classHourType = entity.getClassHourType();
        //定义剩余天数，剩余课时
        Integer surplusAttendanceDay=memberPackage.getSurplusAttendanceDay();

        String memberPackageId=entity.getMemberPackageId();
        //亲子过渡进行加减课时操作
        if (memberPackage.getType() == 1) {
            if (addSubtractType== 1) {
                //进行加赠课课时操作
                Integer surplusPresentClassHour = memberPackage.getSurplusPresentClassHour() + classHour;
                surplusClassHour += classHour;
                row = this.packageService.updateParameterById(ImmutableMap.of(("surplusPresentClassHour"), surplusPresentClassHour, "surplusClassHour", surplusClassHour, "updateTime", new Timestamp(System.currentTimeMillis())), memberPackageId);
            }
            if (addSubtractType == 2) {
                //进行减课时操作
                if (classHourType == 1) {
                    //获取正课课时信息
                    Integer surplusFormalClassHour = memberPackage.getSurplusFormalClassHour() - classHour;
                    surplusClassHour -= surplusClassHour-classHour;
                    row = this.packageService.updateParameterById(ImmutableMap.of(("surplusFormalClassHour"), surplusFormalClassHour, "surplusClassHour", surplusClassHour, "updateTime", new Timestamp(System.currentTimeMillis())),memberPackageId);
                } else if (classHourType == 2) {
                    //获取赠课课时信息
                    Integer surplusPresentClassHour = memberPackage.getSurplusPresentClassHour() - classHour;
                    surplusClassHour -= classHour;
                    row = this.packageService.updateParameterById(ImmutableMap.of(("surplusPresentClassHour"), surplusPresentClassHour, "surplusClassHour", surplusClassHour, "updateTime", new Timestamp(System.currentTimeMillis())), memberPackageId);
                } else if (classHourType == 3) {
                    //获取补课课时信息
                    Integer remediationClassHour = memberPackage.getRemediationClassHour() - classHour;
                    row = this.packageService.updateParameterById(ImmutableMap.of(("remediationClassHour"), remediationClassHour,  "updateTime", new Timestamp(System.currentTimeMillis())), memberPackageId);
                    //生成补课考勤信息
                    saveMemberAttenRecord(entity,memberPackage);

                }
            }
        }else if(memberPackage.getType()==2){
            if (addSubtractType== 1) {
                //进行加课时操作
                int surplusPresentAttendanceDay = memberPackage.getSurplusPresentAttendanceDay() + classHour;
                surplusAttendanceDay += classHour;
                row = this.packageService.updateParameterById(ImmutableMap.of(("surplusPresentAttendanceDay"), surplusPresentAttendanceDay, "surplusAttendanceDay", surplusAttendanceDay, "updateTime", new Timestamp(System.currentTimeMillis())), memberPackageId);
            }
            if (addSubtractType == 2) {
                if (classHourType == 1) {
                    Integer surplusFormalAttendanceDay = memberPackage.getSurplusFormalAttendanceDay() - classHour;
                    surplusAttendanceDay = surplusFormalAttendanceDay + memberPackage.getSurplusPresentAttendanceDay();
                    row = this.tssMemberPackageService.updateParameterById(ImmutableMap.of(
                            ("surplusFormalAttendanceDay"), surplusFormalAttendanceDay,
                            ("surplusAttendanceDay"), surplusAttendanceDay), memberPackageId);
                } else if (classHourType == 2) {
                    Integer surplusPresentAttendanceDay = memberPackage.getSurplusPresentAttendanceDay() - classHour;
                    surplusAttendanceDay = surplusPresentAttendanceDay + memberPackage.getSurplusFormalAttendanceDay();
                    row = this.tssMemberPackageService.updateParameterById(ImmutableMap.of(
                            ("surplusPresentAttendanceDay"), surplusPresentAttendanceDay,
                            ("surplusAttendanceDay"), surplusAttendanceDay), memberPackageId);
                }
            }

        }


        Serializable newId = this.dao.save(entity);
        if (StringUtils.isBlank(newId) && row > 0) {
            return !bool;
        }
        return bool;
    }

    @Override
    public List<Map<String, Object>> getDetailListBymemberId(String memberId, Integer type) {
        return this.dao.getDetailListBymemberId(memberId, type);
    }



    private void saveMemberAttenRecord(TssMemberAddSubtractRecord entity, TssMemberPackage memberPackage) throws Exception {
        TssMemberAttendClassRecord attendClassRecord = new TssMemberAttendClassRecord();
        attendClassRecord.setCreateTime(new Timestamp(System.currentTimeMillis()));
        attendClassRecord.setIsReturnSection(false);
        attendClassRecord.setStatus(1);
        attendClassRecord.setType(memberPackage.getType());
        attendClassRecord.setLessonsId(entity.getClassId());
        attendClassRecord.setIsAttendClass(false);
        Serializable newId = this.tssMemberAttendClassRecordService.save(attendClassRecord);
        TssMemberAttendClassRecord attattendClass = this.tssMemberAttendClassRecordService.get(newId.toString());
        //查询课时相关
        TssMemberPackage tssMemberPackage = tssMemberPackageService.get(entity.getMemberPackageId());
        Map<String, Object> classesLessons = tssClassesLessonsService.findByClassIdCreateTime(entity.getClassId(),entity.getClassTime());
        Integer formalClass = Integer.parseInt(classesLessons.get("formalClass").toString());
        Integer giftClass = Integer.parseInt(classesLessons.get("giftClass").toString());
        TssMemberCheckAttendance tssMemberCheckAttendance = new TssMemberCheckAttendance();
        tssMemberCheckAttendance.setMemberId(tssMemberPackage.getMemberId());
        tssMemberCheckAttendance.setClassId(entity.getClassId());
        tssMemberCheckAttendance.setType(tssMemberPackage.getType());
        tssMemberCheckAttendance.setAttendenceStatus(1);
        tssMemberCheckAttendance.setFormalClass(formalClass);
        tssMemberCheckAttendance.setGiftClass(giftClass);
        tssMemberCheckAttendance.setAdminUserId(entity.getAdminUserId());
        tssMemberCheckAttendance.setCreateTime(entity.getClassTime());
        tssMemberCheckAttendance.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        tssMemberCheckAttendanceService.save(tssMemberCheckAttendance);
    }
}
